class LuckWeaponUI extends UIBase {
	private btn_close:BaseButton;
	private img_icon:eui.Image;
	private btn_get:BaseButton;
	private system:particle.ParticleSystem;
	private _data;
	private item_group:eui.Group;
	private lb_time:eui.Label;
	private lb_effect:eui.Label;
	private lb_tips:eui.Label;
	private isShare;
	public constructor(data?) {
		if(!data)
		{
			data = {}
		}
		data.showBG = true;
		super('LuckWeaponView',data)
	}

	public init()
	{
		super.init(true);
		this.initUI();
	}

	public initUI()
	{
		let weaponId = 0;
		if(this.data.weaponId)
		{
			weaponId = this.data.weaponId;
			this.lb_effect.text = '特殊效果，助你爽快过关!';
			this.lb_time.text = 'x24小时体验';
			this.lb_tips.text = '(立即领取，马上体验)'
			let btn_img:eui.Image = <eui.Image>this.btn_get.getChildByName('btn_img');
			if(btn_img)
			{
				if(Math.random() >= 0.5)
					btn_img.source = 'btn_weaponGet_png';
				else
				{
					this.isShare = true;
					btn_img.source = 'btn_weaponGet_2_png';
				}		
			}
		}
		else
		{
			weaponId = parseInt(GameManager.instance.isCanShowLuckyWeapon);
			GameManager.instance.isCanShowLuckyWeapon = null;
			let btn_img:eui.Image = <eui.Image>this.btn_get.getChildByName('btn_img');
			if(GameManager.instance.isGetLuckyWeaponByShare)
			{
				this.isShare = true;
				btn_img.source = 'btn_weaponGet_2_png';
			}
		}
		this._data = WeaponTpl.instance().getDataByWeaponId(weaponId);
		this.img_icon.source = this._data.itemData.icon;
		this.img_icon.anchorOffsetX = this.img_icon.width >> 1;
        this.img_icon.anchorOffsetY = this.img_icon.height >> 1;
		this.addParticle();
	}

	public addParticle()
	{
		let data = this._data.weaponData;
		if(data && data.effect)
		{
			let texture = RES.getRes(data.effect);
			let config = RES.getRes("particle_52_json");
			config.maxParticles = 8;
			this.system = new particle.GravityParticleSystem(texture, config);
			this.item_group.addChildAt(this.system,0);
			this.system.emitterX = this.item_group.width >> 1;
			this.system.emitterY = this.item_group.height >> 1;
			this.system.start();
		}
	}

	public addEvent()
	{
		this.btn_close.addTapHandler(this.close, this);
		this.btn_get.addTapHandler(this.getWeapon, this);
		GameUtil.addEventListener(EventConst.SHARE_SUCCESS, this.shareSuccess, this);
	}

	public shareSuccess(e:BaseEvent)
	{
		let type = e.param['type'];
		if(type == ShareId.experience_weapon)
        {
            this.request();
        }
	}

	public getWeapon()
	{
		if(!this._data || !this._data.weaponData.id)return;
        if(egret.Capabilities.os == 'Windows PC'){
			this.request();
			return 
		}
		if(!this.data.weaponId)
		{
			if(this.isShare)
			{
				SharePrase.instance().requestOneAdvertisement({type:ShareId.experience_weapon});
			}
			else
			this.request();
		}
		else
		{
			if(this.isShare)
			{
				SharePrase.instance().requestOneAdvertisement({type:ShareId.experience_weapon});
			}
			else
			{
				let self = this;
				let Success_call = function()
				{  
					self.request();
				};
				let Error_call = function()
				{  
					MessageUtil.tips('观看完整视频才能获取幸运武器哦！');
				};
				WeChatgGameManager.instance().showVideoAd(GameConst.adList.other_vedio, Success_call, Error_call, this);
			}
		}
	}

	public request()
	{
		let weaponId = this._data.weaponData.id;
		let send = {
			version:GameConst.version,
			id:weaponId
		}
		let self = this;
		if(!this.data.weaponId)
		{
			//永久获得
			UIManager.instance().showNormalLoading();
			HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorweapon/get_lucky_weapon', StringUtil.jsonToHttpPostString(send), (e)=>{
				let data = JSON.parse(e);
				UIManager.instance().hideNormalLoading();
				if(data && data.msg == 'ok' && data.data)
				{
					let award = data.data;
					RoleCache.instance().curHadWeapon.push(weaponId+'');
					if(award != '')
					GameUtil.openAwardUI(award,null,null);
					RoleCache.instance().curWeaponId = weaponId;
					GameUtil.dispatchEvent(EventConst.CHANGE_WEAPON);
					self.close();
				}
			}, this);
		}
		else
		{
			//一天体验
			UIManager.instance().showNormalLoading();
			HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorweapon/get_experience_weapon', StringUtil.jsonToHttpPostString(send), (e)=>{
				let data = JSON.parse(e);
				UIManager.instance().hideNormalLoading();
				if(data && data.msg == 'ok')
				{
					let award = data.data;		
					if(award != '')
					{
						console.log(award);
						GameUtil.openAwardUI(award,null,null);
					}
					RoleCache.instance().curWeaponId = weaponId;
					GameUtil.dispatchEvent(EventConst.CHANGE_WEAPON);
					self.close();
				}
			}, this);
		}
	}

	public close()
	{
		this.remove(1);
	}

	public removeEvent()
	{
		this.btn_close.removeTapHandler();
		this.btn_get.removeTapHandler();
		GameUtil.removeEventListener(EventConst.SHARE_SUCCESS, this.shareSuccess, this);
	}

	public clear()
	{
		super.clear();
		this._data = null;
		this.isShare = null;
		if(this.system)
		{
			this.system.stop();
			this.system.parent.removeChild(this.system);
			this.system = null;
		}
		let config = RES.getRes("particle_52_json");
		config.maxParticles = 20;
	}
}